package com.meixiang.activity.account;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.meixiang.R;
import com.meixiang.activity.account.manager.FeedBackActivity;
import com.meixiang.dialog.AlertDialogFragment;
import com.meixiang.entity.VersionInfo;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.tool.Tool;
import com.meixiang.util.DataCleanManager;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 账户设置
 * author：wangzuzhen on 2016/5/6 0006 10:33.
 */
public class AccountSettingActivity extends BaseActivity {
    /**
     * 意见反馈
     */
    @Bind(R.id.rl_account_feedback)
    RelativeLayout mRlAccountFeedback;
    /**
     * 版本号
     */
    @Bind(R.id.rl_account_grade)
    RelativeLayout mRlAccountGrade;
    @Bind(R.id.tv_version_number)
    TextView mTvVersionNumber;
    /**
     * 评分
     */
    @Bind(R.id.tv_account_version_number)
    TextView mTvAccountVersionNumber;
    @Bind(R.id.rl_account_version_number)
    RelativeLayout mRlAccountVersionNumber;
    /**
     * 清除缓存
     */
    @Bind(R.id.rl_account_cache)
    RelativeLayout mRlAccountCache;
    @Bind(R.id.tv_cache)
    TextView mTvCache;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_account_setting);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        setTitle(getResources().getString(R.string.account_setting));
    }

    @Override
    protected void initView() {

        mTvVersionNumber.setText("V"+Tool.getAppVersionName(context));
        try {
            mTvCache.setText(DataCleanManager.getTotalCacheSize(context));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void setData() {

    }


    @OnClick({R.id.rl_account_feedback, R.id.rl_account_grade, R.id.rl_account_version_number, R.id.rl_account_cache})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_account_feedback:
                startActivity(new Intent(context, FeedBackActivity.class));
                break;
            case R.id.rl_account_grade:
                break;
            case R.id.rl_account_version_number:
                checkVersion(1);
                break;
            case R.id.rl_account_cache:
                new AlertDialogFragment.Builder()
                        .setContentText("确认清除本地缓存？")
                        .setLeftBtnText("确认")
                        .setRightBtnText("取消")
                        .setLeftClickCallBack(new AlertDialogFragment.LeftClickCallBack() {
                            @Override
                            public void dialogLeftBtnClick() {
                                DataCleanManager.clearAllCache(context);
                                try {
                                    mTvCache.setText(DataCleanManager.getTotalCacheSize(context));
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        })
                        .build()
                        .show(getSupportFragmentManager(), AlertDialogFragment.TAG);

                break;
        }
    }

    /**
     *
     * @param type  0:启动时检测更新
     *              1：点击检测更新
     */
    private void checkVersion(final int type) {
        HttpUtils.get(Config.VERSIONS_UPDATA, new HttpCallBack(this) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                final VersionInfo info=new Gson().fromJson(result.toString(),VersionInfo.class);
                String currentVersion=Tool.getAppVersionName(context);
                if (Tool.compare(info.getVersion(), currentVersion) == 1) {
                    AlertDialog.Builder dialog = new AlertDialog.Builder(context);
                    dialog.setTitle("发现新版本" + info.getVersion());
                    dialog.setMessage(info.getContent());
                    dialog.setCancelable(false);
                    dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                            if (type == 0) {
                                if (info.getIsForcedUpdating().equals("true")){
                                    finish();
                                }else{
                                    dialog.cancel();
                                }
                            }else{
                                dialog.cancel();
                            }
                        }
                    });
                    dialog.setPositiveButton("更新", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(info.getUrl()));
                            startActivity(intent);
                        }
                    });
                    dialog.show();
                } else {
                    if (type == 0) {

                    } else if (type == 1) {
                        Toast.makeText(context, "已是最新版本", Toast.LENGTH_LONG).show();
                    }
                }
            }

            @Override
            public void onError(String code, String message) {

            }
        });

    }
}
